﻿Public Class clsProdInfo

    Public Function getProdInfoListByTrayNoAndStatus(tray_no As String, status As String) As DataTable
        Dim szSql As String = ""
        szSql &= "SELECT * FROM sf.b_prod_info WHERE 1=1 and tray_no = ? and status = ?"
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, tray_no, status)
        Return dt
    End Function

    Public Function insert(en As entity_YZZ) As Integer
        Dim szSql As String = String.Empty
        Dim rtn As Integer = 0
        Try
            DBacs.BeginTransaction()
            szSql &= "INSERT INTO sf.b_prod_info( factory_id , prod_id , gjb_sys_id , gjb_dl_no , flb_sys_id , flb_dl_no , jp1wt , tzgb_wt ,  bbp_wt , gqncj_wt , bhj_wt , status , set_bhj_ope , update_time , prod_spec_id , jp_type , steel_seal_no,gqbhj_wt,jstz_wt,b_pgd_yzz_details_sys_id,rv_ope, location) "
            szSql &= "VALUES ( ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? ,? , ?, ?, ?, ?)"
            With en
                rtn = DBacs.ExecuteUpdateSql(szSql, -1, .factory_id, .prod_id, .gjb_sys_id, .gjb_dl_no, .flb_sys_id, .flb_dl_no, .jp1wt, .tzgb_wt, .bbp_wt, .gqncj_wt, .bhj_wt, .status, .set_bhj_ope, .update_time, .prod_spec_id, .jp_type, .steel_seal_no, .gqbhj_wt, .jstz_wt, .b_pgd_yzz_details_sys_id, .rv_ope, .location)
            End With
            '入库状态变更
            If String.IsNullOrEmpty(en.location) = False Then
                szSql = "INSERT INTO sf.b_lh_cache_area( factory_id, wh_no, prod_id, into_time, status, into_ope,  update_time)  "
                szSql &= "VALUES ( ?, ?, ?, (SELECT CURRENT_TIMESTAMP(6)), ?, ?, (SELECT CURRENT_TIMESTAMP(6)))"
                With en
                    rtn = DBacs.ExecuteUpdateSql(szSql, -1, .factory_id, .location, .prod_id, "0", .rv_ope)
                End With
                szSql = "update sf.b_prod_info "
                szSql &= "set update_time=(SELECT CURRENT_TIMESTAMP(6)),status = ?,in_st_ope = ?, storage_time = (SELECT CURRENT_TIMESTAMP(6)) "
                szSql &= "where factory_id = ? and prod_id = ? "
                rtn = DBacs.ExecuteUpdateSql(szSql, -1, "3", en.rv_ope, en.factory_id, en.prod_id)
            End If
            '法兰板状态变更
            szSql = "update sf.b_flb_info "
            szSql &= "set used_piece_num = used_piece_num + 1,status = ?,used_date=(SELECT CURRENT_TIMESTAMP(6)) "
            szSql &= "where sys_id = ? "
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, IIf(en.flbnum = 1, "2", "1"), en.flb_sys_id)

            '骨架板状态变更 
            szSql = "update sf.b_gjb_dl_info "
            szSql &= "set status = ?, used_date = (SELECT CURRENT_TIMESTAMP(6)) "
            szSql &= "where sys_id = ? "
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, "2", en.gjb_sys_id)

            '料框吊篮主档空满状态变更
            szSql = "update sf.t_mst_dl set status = ?,update_time=(SELECT CURRENT_TIMESTAMP(6)) "
            szSql &= "where dl_no = ? "
            '骨架板空满状态变更
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, "0", en.gjb_dl_no.Substring(0, en.gjb_dl_no.Length - 1))
            '法兰板空满状态变更
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, IIf(en.flbnum > 0, "2", "0"), en.flb_dl_no)

            '预组装库位空满状态变更
            If String.IsNullOrEmpty(en.location) = False Then
                szSql = "update sf.t_mst_storage_location set status = ?,update_time=(SELECT CURRENT_TIMESTAMP(6)) "
                szSql &= "where location_no = ? "
                rtn = DBacs.ExecuteUpdateSql(szSql, -1, "1", en.location)
            End If

            '生产计划完成数变更
            szSql = "update sf.b_pgd_mes "
            szSql &= "set finish_count = ?, update_time = (SELECT CURRENT_TIMESTAMP(6)) "
            szSql &= "where sys_id = ? "
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, "2", en.b_pgd_yzz_details_sys_id)
            DBacs.CommitTransaction()
        Catch ex As Exception
            DBacs.RollbackTransaction()
            rtn = -2
        End Try
        Return rtn
    End Function

    Public Function getProdInfoListByStatus(status As String) As DataTable
        Dim szSql As String = ""
        szSql &= "SELECT * FROM sf.b_prod_info WHERE status = ?"
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1, status)
        Return dt
    End Function

    Public Function getProdInfoListByProject(project_code As String, project_name As String) As DataTable
        Dim szSql As String = "SELECT a.*,b.mes_project_name,b.mes_project_code,c.prod_spec_name FROM sf.b_prod_info a "
        szSql &= " left join sf.b_pgd_mes b on a.b_pgd_yzz_details_sys_id= b.sys_id "
        szSql &= " left join sf.t_prod_spec c on a.prod_spec_id= c.prod_spec_id "
        szSql &= " WHERE b.mes_project_code like '%" & project_code & "%' and b.mes_project_name like '%" & project_name & "%'"
        Dim dt As DataTable = DBacs.ExecuteSql(szSql, -1)
        Return dt
    End Function

    ' 更新预组装信息
    Public Function updateProdInfo(prodId As String, gjb_sys_id As String, gjb_dl_no As String, jp_barcode As String, jp_layer_num As String, jp_all_wt As String, jp_type As String, opers As String) As Integer
        Dim szSql As String = String.Empty
        Dim rtn As Integer
        DBacs.BeginTransaction()
        Try
            szSql &= " update sf.b_prod_info set status = ?, "
            szSql &= " gjb_sys_id = ?, gjb_dl_no = ?,"
            If String.IsNullOrEmpty(jp_barcode) = False Then
                szSql &= "jp_barcode = ?,"
            End If
            szSql &= " jp_all_wt = ?, jp_type = ?, jp_layer_num = ?, zz_ope = ?, zz_time = (SELECT CURRENT_TIMESTAMP(6)), "
            szSql &= " update_time = (SELECT CURRENT_TIMESTAMP(6))"
            szSql &= " where prod_id = ? "
            If String.IsNullOrEmpty(jp_barcode) = False Then
                rtn = DBacs.ExecuteUpdateSql(szSql, -1, "1", gjb_sys_id, gjb_dl_no, jp_barcode, jp_all_wt, jp_type, jp_layer_num, opers, prodId)
            Else
                rtn = DBacs.ExecuteUpdateSql(szSql, -1, "1", gjb_sys_id, gjb_dl_no, jp_all_wt, jp_type, jp_layer_num, opers, prodId)
            End If

            If String.IsNullOrEmpty(jp_barcode) = False Then
                szSql = "update sf.b_jp_wt_master set status = ?,used_time=(SELECT CURRENT_TIMESTAMP(6)),used_name=? where pkey = ?"
                rtn = DBacs.ExecuteUpdateSql(szSql, -1, "1", opers, jp_barcode)
            End If

            '更新骨架板吊篮主档忙闲字段
            Dim gjb_dl_noo As String = gjb_dl_no.Substring(0, gjb_dl_no.Length - 1)
            szSql = "update sf.t_mst_dl set status = ?,update_time=(SELECT CURRENT_TIMESTAMP(6)) "
            szSql &= "where dl_no = ? "
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, "0", gjb_dl_noo)
            '骨架板状态变更 
            szSql = "update sf.b_gjb_dl_info "
            szSql &= "set status = ?, used_date = (SELECT CURRENT_TIMESTAMP(6)) "
            szSql &= "where sys_id = ? "
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, "2", gjb_sys_id)
            szSql = String.Empty
            szSql &= "update sf.b_workshop_current "
            szSql &= "set finished_quantity = finished_quantity + 1,update_time=(SELECT CURRENT_TIMESTAMP(6)) "
            szSql &= "where factory_id = ? and workshop = ? and current_section = ?"
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, "TS", 0, "YZZSG")
            DBacs.CommitTransaction()
        Catch ex As Exception
            rtn = -2
            DBacs.RollbackTransaction()
        End Try
        Return rtn
    End Function

    Public Function updateProdInfoJpAllWt(prodId As String, jp_all_wt As String)
        Dim szSql As String = String.Empty
        Dim rtn As Integer
        DBacs.BeginTransaction()
        Try
            szSql &= " update sf.b_prod_info set jp_all_wt = ? where prod_id = ? "
            rtn = DBacs.ExecuteUpdateSql(szSql, -1, jp_all_wt, prodId)
            DBacs.CommitTransaction()
        Catch ex As Exception
            rtn = -2
            DBacs.RollbackTransaction()
        End Try
        Return rtn
    End Function

End Class
